package com.Listen;

import javax.servlet.*;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/*
当对域属性进行增、删、改时，执行的监听器一共有三个：

l  ServletContextAttributeListener：在ServletContext域进行增、删、改属性时调用下面方法。

Ø public void attributeAdded(ServletContextAttributeEvent evt)

Ø public void attributeRemoved(ServletContextAttributeEvent evt)

Ø public void attributeReplaced(ServletContextAttributeEvent evt)

l HttpSessionAttributeListener：在HttpSession域进行增、删、改属性时调用下面方法

Ø public void attributeAdded(HttpSessionBindingEvent evt)

Ø public void attributeRemoved (HttpSessionBindingEvent evt)

Ø public void attributeReplaced (HttpSessionBindingEvent evt)

l ServletRequestAttributeListener：在ServletRequest域进行增、删、改属性时调用下面方法

Ø public void attributeAdded(ServletRequestAttributeEvent evt)

Ø public void attributeRemoved (ServletRequestAttributeEvent evt)

Ø public void attributeReplaced (ServletRequestAttributeEvent evt)



下面对这三个监听器的事件对象功能进行介绍：

l ServletContextAttributeEvent

Ø String getName()：获取当前操作的属性名；

Ø Object getValue()：获取当前操作的属性值；

Ø ServletContext getServletContext()：获取ServletContext对象。

l HttpSessionBindingEvent

Ø String getName()：获取当前操作的属性名；

Ø Object getValue()：获取当前操作的属性值；

Ø HttpSession getSession()：获取当前操作的session对象。

l ServletRequestAttributeEvent

Ø String getName()：获取当前操作的属性名；

Ø Object getValue()：获取当前操作的属性值；

Ø ServletContext getServletContext()：获取ServletContext对象；

Ø ServletRequest getServletRequest()：获取当前操作的ServletRequest对
* */
public class MyListener implements ServletContextAttributeListener,
        ServletRequestAttributeListener,
        HttpSessionAttributeListener {
    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        System.out.println("session attibute 被创建"+event.getValue()+event.getName());
    }

    @Override
    public void attributeAdded(ServletRequestAttributeEvent srae) {
        System.out.println("request attibute 被创建"+srae.getValue()+srae.getName());
    }

    @Override
    public void attributeAdded(ServletContextAttributeEvent event) {
        System.out.println("contest attibute 被创建"+event.getName()+event.getValue());
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        System.out.println("session attibute 被销毁"+event.getName()+event.getValue());
    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent srae) {
        System.out.println("request attibute 被销毁"+srae.getName()+srae.getValue());
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent event) {
        System.out.println("context attibute 被销毁"+event.getValue()+event.getName());
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {
        System.out.println("session attibute 被替代");
    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent srae) {
        System.out.println("request attibute 被替代");
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent event) {
        System.out.println("contest attibute 被替代");
    }
}
